Knowledgebase Home Page  >  RapidSpell Web ASP.NET  >  Behavior Customization
Search the Knowledge Base
How can I use a database based user dictionary instead of the default file based dictionary? (C#, VB.NET)
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=128&catId=55

Options

Print this page
Email this to a friend

By default, setting the .UserDictionaryFile property in the controls to a file path will mean that when the user clicks "Add" in the GUI, that the word is stored in that file.  To instead work with database based user dictionaries you can specify your own descendant of our UserDictionary class, to be used as the user dictionary.  This means of course that you can override the behavior in order to work with databases or anything else.

 

This class is just a skeleton that needs the actual DB implementation code to be added. This code need only read the word list from a table, and add new words to the list/DB. The example shows how a dictionary identifier (dictionaryId) can be passed, however this isn't mandatory and all users can share the same dictionary. Although, the identifier could be used to decide who can add words and who cannot etc.

In the example code below, a dummy list wordList holds a preset dictionary - your task will be to pull a list of words from your database and populate the list.  The AddWord method is also responsible for writing the new word to your DB.

 

 

DBUserDictionary Class



C#
===

using System;

using System.Collections;

using System.Data.SqlClient;

using System.Data;

namespace CustomUserDic

{

///

/// Extends and customises UserDictionary functionality.

/// This is a simple example class, to demonstrate the principle, your own implementation

/// should handle your connections, db setup and exceptions as per the rest of your

/// application.

///

public class DBUserDictionary : Keyoti.RapidSpell.UserDictionary

{

ArrayList wordList;

int wordLimit;

String dictionaryId;

public DBUserDictionary(String dictionaryId, int maxNumWords)

{

wordList = new ArrayList(50);

wordLimit = maxNumWords;

this.dictionaryId = dictionaryId;

//Commented out to simplify and test;

//The purpose of this is to read all user words stored in the DB into the list

//SqlConnection conn = new SqlConnection("Persist Security Info=False;Integrated Security=SSPI;database=pb_mcsis;server=localhost;Connect Timeout=30");

//SqlCommand command = new SqlCommand("select * From DictionaryUser", conn);

//conn.Open();

//SqlDataReader dr = command.ExecuteReader(CommandBehavior.SingleResult);

//while(dr.Read())

// wordList.Add(dr[0]);

//conn.Close();

wordList.Add("ProgressBook");

wordList.Add("Paul");

wordList.Add("Dale");

wordList.Add("WordsBySA");

}

 

public DBUserDictionary(String dictionaryId) : this(dictionaryId, 100000) { }

 

 

public override int ReadAll(ArrayList a)

{

//used by RapidSpell to read the words from the DB.

a.Clear();

wordlist.Sort();

a.AddRange(wordList);

return a.Count;

}

 

public override bool IsValid()

{

//return true if the word list exists and is valid.

//this should return false, if for example there was an sql exception at some point.

//return wordList != null;

//Defaulted to True for test.

return true;

}

public override bool ReloadDictionary()

{

//doing anything necessary here

return true;

}

public override bool AddWord(String word)

{

wordList.Add(word);

OnListChanged(new ListChangedEventArgs(ListChangedEventArgs.ListChangeOperation.WordAdd, word));

//The purpose of this is to store the word (in "word") in the database.

// SqlConnection conn = new SqlConnection("server=(local)\\NetSDK;database=....;Trusted_Connection=yes");

// SqlCommand command = new SqlCommand("INSERT INTO .............", conn);

// conn.Open();

// command.ExecuteNonQuery();

// conn.Close();

return true;

}

public override bool RemoveWord(string word)

{

wordList.Remove(word);

OnListChanged(new ListChangedEventArgs(ListChangedEventArgs.ListChangeOperation.WordRemove, word));

return base.RemoveWord(word);

}

}

}






VB.NET
=======

Imports System
Imports System.Collections
Imports System.Data.SqlClient
Imports System.Data


Namespace CustomUserDic
_
'/


'/ Extends and customises UserDictionary functionality.
'/ This is a simple example class, to demonstrate the principle, your own implementation
'/ should handle your connections, db setup and exceptions as per the rest of your
'/ application.
'/

Public Class DBUserDictionary
Inherits Keyoti.RapidSpell.
UserDictionary

Private wordList As ArrayList
Private wordLimit As Integer
Private dictionaryId As [String]


Public Sub New(dictionaryId As [String], maxNumWords As Integer)
wordList = New ArrayList(50)
wordLimit = maxNumWords
Me.dictionaryId = dictionaryId

'Commented out to simplify and test;
'The purpose of this is to read all
 user words stored in the DB into the list
Dim conn As New SqlConnection("Persist Security Info=False;Integrated Security=SSPI;
database=pb_mcsis;server=localhost;Connect Timeout=30")
Dim command As New SqlCommand("select * From Dictionary
User", conn)

conn.Open()
Dim dr As SqlDataReader = command.ExecuteReader(CommandBehavior.SingleResult)
While dr.Read()
wordList.Add(dr(0))
End While
conn.Close()
End Sub 'New





Public Sub New(dictionaryId As [String])
MyClass.New(dictionaryId, 100000)
End Sub 'New
Public Overrides Function ReloadDictionary() As Boolean

'doing anything necessary here

Return True

End Function



Public Overrides Function ReadAll(a As ArrayList) As Integer
'used by RapidSpell to read the words from the DB.
a.Clear()
a.AddRange(wordList)
Return a.Count
End Function 'ReadAll



Public Overrides Function IsValid() As Boolean
'return true if the word list exists and is valid.
'this should return false, if for example there was an sql exception at some point.
'return wordList != null;
'Defaulted to True for test.
Return True
End Function 'IsValid


Public Overrides Function AddWord(w As [String]) As Boolean
wordList.Add(w)

'The purpose of this is to store the word (in "w") in the database.
Dim conn As New SqlConnection("server=(local)\NetSDK;
database=....;Trusted_Connection=yes")
Dim command As New SqlCommand("INSERT INTO .............", conn)
conn.Open()
command.ExecuteNonQuery()
conn.Close()
Return True
End Function 'AddWord
End Class 'DB
UserDictionary
End Namespace 'Custom
UserDic

 

Using The DBUserDictionary Class

The new subclass of UserDictionary is then used like this:

If you are using the Javascript approach in version 4 up:

For Inline/As-you-type: modify the code in the page holding RapidSpellWInlineHelper, this is named “rswihelper.aspx” and is present in the Keyoti_RapidSpell_Web_Common folder under your web app.

Keyoti.RapidSpell.RapidSpellWInlineHelper rapidSpellWInlineHelper1 = new Keyoti.RapidSpell.RapidSpellWInlineHelper();

rapidSpellWInlineHelper1.CheckerEngine.SetUserDictionary( new DBUserDictionary ());

Form1.Controls.Add(rapidSpellWInlineHelper1);

 

 

For Dialog:  modify the code in the page holding RapidSpellWeb (the popup), this is named “popup.aspx” and is present in the Keyoti_RapidSpell_Web_Common folder under your web app.

Keyoti.RapidSpell.RapidSpellWeb rapidSpellWeb1 = new Keyoti.RapidSpell.RapidSpellWeb();

rapidSpellWeb1.SSLFriendly = true;

rapidSpellWeb1.Width = new Unit(100, UnitType.Percentage);

RapidSpellWeb1.UserDictionary = new DBUserDictionary ();

Form1.Controls.Add(rapidSpellWeb1);

 

 

 

If you are using the Control based approach in all versions:

For RapidSpellWInline - modify the codebehind in the page holding RapidSpellWInlineHelper1

RapidSpellWInlineHelper1.CheckerEngine.SetUserDictionary( new DBUserDictionary ());

you must also set RapidSpellWInline.ShowAddItemAlways=true

 

For RapidSpellWebLauncher/RapidSpellWeb - modify the codebehind in the page holding RapidSpellWeb (the popup)

RapidSpellWeb1.UserDictionary =  new DBUserDictionary ();

 

 

 

 
Email support@keyoti.com if you have any questions please.

Related Questions:

Attachments:

No attachments were found.